package com.xmkj.bearuser.mine.activity;

import android.view.View;
import android.widget.EditText;

import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.UpdateNameMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.widget.navigation.WidgetButton;
import com.xmkj.bearuser.R;

public class ModifyNameActivity extends BaseMvpActivity {
    public static final String NAME = "name";
    EditText etName;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_modify_name;
    }

    @Override
    protected void onViewCreated() {
        etName = findViewById(R.id.et_name);
        String name = getIntent().getStringExtra(NAME);
        if (EmptyUtils.isNotEmpty(name)) {
            etName.setText(name);
        }
        setRightNavigationText();
    }

    @Override
    protected void doLogicFunc() {

    }

    @Override
    protected void setNavigation() {
        setNavigationBack("昵称");
    }

    private void setRightNavigationText() {
        m_navigationBar.setVisibility(View.VISIBLE);
        WidgetButton widgetButton = new WidgetButton(this, R.string.save);
        widgetButton.getTextView().setTextColor(getResources().getColor(R.color.navigation_background_color));
        widgetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EmptyUtils.isEmpty(getEditTextStr(etName))) {
                    showToastMsg("请填写姓名");
                    return;
                }
                gotoUpdateUser();
            }
        });
        m_navigationBar.setRightMenu(widgetButton);
    }

    private void gotoUpdateUser() {
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {

                dismissProgressDialog();
                RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.UPDATE_NAME_SUCCESS, getEditTextStr(etName)));
                RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.MODIFYSUCCESS, true));
                onBackPressed();


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                /*               swipRefresh.setRefreshing(false);*/
                showToastMsg(e);
            }
        });
        uid = SPUtils.getShareInt(Constans.UID);
        hashid =  SPUtils.getShareString(Constans.HASHID);
        UpdateNameMethods.getInstance().updateNickname(subscriber, uid, hashid,getEditTextStr(etName));
        rxManager.add(subscriber);
    }
}
